package com.flow.demo.module.gd.controller.app.good;

import com.flow.demo.module.gd.controller.app.good.vo.GoodPageReqVO;
import com.flow.demo.module.gd.controller.app.good.vo.GoodRespVO;
import com.flow.demo.module.gd.controller.app.good.vo.GoodSaveReqVO;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import com.flow.demo.framework.common.pojo.PageParam;
import com.flow.demo.framework.common.pojo.PageResult;
import com.flow.demo.framework.common.pojo.CommonResult;
import com.flow.demo.framework.common.util.object.BeanUtils;
import static com.flow.demo.framework.common.pojo.CommonResult.success;

import com.flow.demo.framework.excel.core.util.ExcelUtils;

import com.flow.demo.framework.operatelog.core.annotations.OperateLog;
import static com.flow.demo.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.flow.demo.module.gd.dal.dataobject.good.GoodDO;
import com.flow.demo.module.gd.service.good.GoodService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 奖品")
@RestController
@RequestMapping("/gd/good")
@Validated
public class GoodController {

    @Resource
    private GoodService goodService;

    @PostMapping("/create")
    @Operation(summary = "创建奖品")
    @PreAuthorize("@ss.hasPermission('gd:good:create')")
    public CommonResult<Integer> createGood(@Valid @RequestBody GoodSaveReqVO createReqVO) {
        return success(goodService.createGood(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新奖品")
    @PreAuthorize("@ss.hasPermission('gd:good:update')")
    public CommonResult<Boolean> updateGood(@Valid @RequestBody GoodSaveReqVO updateReqVO) {
        goodService.updateGood(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除奖品")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('gd:good:delete')")
    public CommonResult<Boolean> deleteGood(@RequestParam("id") Integer id) {
        goodService.deleteGood(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得奖品")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('gd:good:query')")
    public CommonResult<GoodRespVO> getGood(@RequestParam("id") Integer id) {
        GoodDO good = goodService.getGood(id);
        return success(BeanUtils.toBean(good, GoodRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得奖品分页")
    @PreAuthorize("@ss.hasPermission('gd:good:query')")
    public CommonResult<PageResult<GoodRespVO>> getGoodPage(@Valid GoodPageReqVO pageReqVO) {
        PageResult<GoodDO> pageResult = goodService.getGoodPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, GoodRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出奖品 Excel")
    @PreAuthorize("@ss.hasPermission('gd:good:export')")
    @OperateLog(type = EXPORT)
    public void exportGoodExcel(@Valid GoodPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<GoodDO> list = goodService.getGoodPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "奖品.xls", "数据", GoodRespVO.class,
                        BeanUtils.toBean(list, GoodRespVO.class));
    }

}
